<template>
  <!-- 添加或修改订单提成规则对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="地区" prop="areas">
                <el-select v-model="form.areas" multiple placeholder="请选择地区" @change="change()" style="width: 100%;">
                  <el-option v-for="dict in dict.type.order_area" :key="dict.value"
                             :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回款天数最小值" prop="returnMin">
                <el-input-number v-model="form.returnMin" :min=0 :max="form.returnMax" placeholder="请输入回款天数最小值"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回款天数最大值" prop="returnMax">
                <el-input-number v-model="form.returnMax"  :min="form.returnMin"  placeholder="请输入回款天数最大值"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="提成百分比" prop="percent">
                <el-input-number v-model="form.percent" :precision="2" placeholder="请输入提成百分比"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否有卸车费" prop="unloadFee">
                <el-radio-group v-model="form.unloadFee">
                  <el-radio v-for="dict in dict.type.yes_no"
                            :key="dict.value" :label="dict.value">{{dict.label}}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';

export default {
  name: "OrderCommissionRuleForm",
  dicts: ['order_area','yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        areas: [],
      },
      // 表单校验
      rules: {
        areas: [
          {required: true, message: "请选择", trigger: "blur"}
        ],
        returnMin: [
          {required: true, message: "回款最小天数", trigger: "blur"}
        ],
        percent: [
          {required: true, message: "提成百分比不能为空", trigger: "blur"}
        ],
        unloadFee: [
          {required: true, message: "是否有卸车费不能为空", trigger: "blur"}
        ],
      },
      url: {
        getById: '/orderCommissionRule/orderCommissionRule/',
        add: '/orderCommissionRule/orderCommissionRule/add',
        edit: '/orderCommissionRule/orderCommissionRule/edit',
      },
    };
  },
  created() {

  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        area: null,
        returnMin: null,
        returnMax: null,
        percent: null,
        unloadFee: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单提成规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.form.areas = response.data.area.split(",");
        this.open = true;
        this.title = "修改订单提成规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.area = this.form.areas.toString();
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },

    change(){
      this.$forceUpdate();
    }
  }
};
</script>
